﻿using System.Collections;
using System.Collections.Generic;
using System;

public struct Arg
{
    private string _key;
    private object _value;

    public string Key
    {
        get
        {
            return _key;
        }
        set
        {
            _key = value;
        }
    }
    public object Value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
        }
    }

    public Arg(string key, object value)
    {
        _key = key;
        _value = value;
    }
}

public class Table : IEnumerable
{
    #region Fields and Properties
    public List<Arg> list = new List<Arg>();
    #endregion

    #region Constructors
    public Table()
    {
    }
    public Table(params Arg[] args)
    {
        for (int i = 0; i < args.Length; i++)
            list.Add(args[i]);
    }
    #endregion

    #region Public methods
    public object this[string key]
    {
        get { return GetArgByKey(key).Value; }
    }
    public Nullable<Arg> GetArgByKey(string key)
    {
        foreach (Arg a in list)
            if (a.Key == key)
                return a;
        return null;
    }
    public int GetIndexByKey(string key)
    {
        for (int i = 0; i < list.Count; i++)
            if (list[i].Key == key)
                return i;
        return -1;
    }
    #endregion

    #region IEnumerable Members

    public IEnumerator GetEnumerator()
    {
        return new TableEnum(list);
    }

    #endregion
}

public class TableEnum : IEnumerator

{
    public List<Arg> list;

    // Enumerators are positioned before the first element
    // until the first MoveNext() call.
    int position = -1;

    public TableEnum(List<Arg> list)
    {
        this.list = list;
    }

    public bool MoveNext()
    {
        position++;
        return (position < list.Count);
    }

    public void Reset()
    {
        position = -1;
    }

    public object Current
    {
        get
        {
            try
            {
                return list[position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }

}


